package com.tim.app.ui.adapter.found;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.server.entry.Story;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 播放列表适配器
 */
public class PlayListAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Story> {

    static final String TAG = "PlayListAdapter";

    private Context mContext;

    private int currentIndex;

    public PlayListAdapter(Context mContext, List<Story> mDataList) {
        super(mDataList);
        this.mContext = mContext;
        currentIndex = 0;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_play_list, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder mHolder, int position, final Story data) {

        if (null == data)
            return;
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvTitle.setText(data.getTitle());
        } else {
            holder.tvTitle.setText("");
        }
        BitmapLoader.ins().loadImage(data.getImage(), holder.rivImage);
        holder.tvPlaycount.setText(String.valueOf(data.getPvCount()));
        holder.tvTotalTime.setText(data.getTime());
        if (position == getCurrentIndex()) {
            holder.tvTitle.setSelected(true);
            holder.ivStated.setSelected(true);
        } else {
            holder.tvTitle.setSelected(false);
            holder.ivStated.setSelected(false);
        }
    }


    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivImage;
        ImageView ivStated;
        TextView tvTitle, tvPlaycount, tvTotalTime;

        public ViewHolder(View itemView) {
            super(itemView);
            rivImage = (RoundedImageView) itemView.findViewById(R.id.rivImage);
            ivStated = (ImageView) itemView.findViewById(R.id.ivStated);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvPlaycount = (TextView) itemView.findViewById(R.id.tvPlaycount);
            tvTotalTime = (TextView) itemView.findViewById(R.id.tvTotalTime);
        }

    }

}
